package mahjong;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JOptionPane;

/**
 * This class is a list of highscores for a specific layout
 *
 */
public class HighScoreList implements Serializable {

	private String layout;
	
	private ArrayList<Score> topscores;
	
	
	public HighScoreList(String l)
	{
		layout = l;
		topscores = new ArrayList<Score>();
	}
	
	public void addHighScore(Score s)
	{
		if(topscores.size() < 10)
		{
			topscores.add(s);
			Collections.sort(topscores);
		}
		else
		{
			topscores.add(s);
			Collections.sort(topscores);
			topscores.remove(10);
		}
	}
	
	public String getHighScores()
	{
		if(topscores.size() == 0) return "No highscores for this layout!";
		
		String highscores = "Highscores: \n";
		for(Score s : topscores)
		{
			highscores += s.getName()+" : "+ TimeFormater.formatTime(s.getScore()) +"\n";
		}
		
		return highscores;
	}
	
	public String getLayout()
	{
		return layout;
	}
	
	public boolean canAdd(int time)
	{
		if(topscores.size() < 10) return true;
		
		for(Score s : topscores)
		{
			if(s.getScore() > time) return true;
		}
		
		return false;
	}
	
}
